package com.sinodata.bsm.cicp.finder.tuxedo;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.SnmpUtil;

public class TuxedoClientFinder extends Finder {
    private static Logger logger = Logger.getLogger(TuxedoClientFinder.class);

    /*public List find(ResValue resValue, ResAttributeValValue[] attrVals, ResAttributeValue[] attributes) {
        List retList = new ArrayList();
        String result = null;
        BufferedReader reader = null;
        String ip = host;
        String TableOIDS = ".1.3.6.1.4.1.140.300.17.1.1.3,.1.3.6.1.4.1.140.300.17.1.1.5";
        String community = "public";
        String regex = "\\s*(.*)#(.*)#";
        Pattern pattern = Pattern.compile(regex);
        try {
            result = SnmpConnect.getValueBySnmpTable(ip, TableOIDS, community);
            StringReader strReader = new StringReader(result);
            reader = new BufferedReader(strReader);
            String readLine = null;
            while ((readLine = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(readLine);
                if (matcher.find()) {
                    retList.add(creatRes(matcher, attributes));
                }
            }
            logger.info("tuxedo server find successed");
        } catch (Exception e) {
            logger.error("failed to find tuxedo server", e);
        }
        System.out.println(retList.size());
        return retList;
    }*/

    /* private List creatRes(Matcher matcher, ResAttributeValue[] attributes) {
         List aRes = new ArrayList();
         ResValue resValue = new ResValue();
         resValue.setIp(host);
         resValue.setName(matcher.group(2));
         resValue.setInternalId(matcher.group(2));
         aRes.add(resValue);
         aRes.add(CollectorUtil.getResAttrValValue(attributes[0].getId(), matcher.group(2)));
         aRes.add(CollectorUtil.getResAttrValValue(attributes[1].getId(), matcher.group(1)));
         return aRes;
     }*/

    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String result = null;
        BufferedReader reader = null;
        String tableOIDS = ".1.3.6.1.4.1.140.300.17.1.1.3,.1.3.6.1.4.1.140.300.17.1.1.5";
        String community = "public";
        String regex = "\\s*(.*)#(.*)#";
        Pattern pattern = Pattern.compile(regex);
        try {
            result = SnmpUtil.getValueBySnmpTable(ip, tableOIDS, community);
            StringReader strReader = new StringReader(result);
            reader = new BufferedReader(strReader);
            String readLine = null;
            while ((readLine = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(readLine);
                if (matcher.find()) {
                    String[] info = new String[5];
                    list.add(info);
                    info[0] = matcher.group(2);
                    info[1] = matcher.group(2);
                    info[2] = matcher.group(2);
                    info[3] = matcher.group(2);
                    info[4] = matcher.group(1);
                }
            }
            return list;
        } catch (Exception e) {
            logger.error("failed to find tuxedo client", e);
            return null;
        }
    }

}
